package com.example.bookapp.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
@@ -10,17 +11,21 @@ import android.widget.EditText;

        import com.example.txtapp.R;

        import org.jetbrains.annotations.NotNull;
        import org.json.JSONException;
        import org.json.JSONObject;

        import java.io.IOException;
        import java.io.InputStream;
        import java.io.OutputStream;
        import java.net.HttpURLConnection;
        import java.net.MalformedURLException;
        import java.net.URL;

        import javax.sql.StatementEvent;
        import okhttp3.Call;
        import okhttp3.Callback;
        import okhttp3.FormBody;
        import okhttp3.MediaType;
        import okhttp3.OkHttpClient;
        import okhttp3.Request;
        import okhttp3.RequestBody;
        import okhttp3.Response;


public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = "RegisterActivity";
    @@ -39,39 +44,34 @@ public class RegisterActivity extends AppCompatActivity {
        mBtRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url="http://192.168.0.102:8080/BOOKAPP/user/register";
                try {
                    URL url1=new URL(url);
                    HttpURLConnection conn= (HttpURLConnection) url1.openConnection();
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Content-type","application/json");
                    JSONObject object=new JSONObject();
                    object.put("name",mEtName1.getText());
                    object.put("telephone",mEtTelephone1.getText());
                    object.put("password",mEtPassword1.getText());
                    String data=object.toString();

                    //发送数据
                    OutputStream writer=conn.getOutputStream();
                    writer.write(data.getBytes());
                    //接收处理数据
                    InputStream inputStream=conn.getInputStream();
                    byte[] bytes=new byte[1024];
                    int len=inputStream.read(bytes);
                    String message=new String(bytes,0,len);

                    Log.e(TAG, "onClick: "+message );

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                //创建client
                OkHttpClient client=new OkHttpClient();
                //创建Request准备数据
                RequestBody body=new FormBody.Builder()
                        .add("name","tom")
                        .add("telephone","1234")
                        .add("password","1234")
                        .build();
                //创建request
                Request request=new Request.Builder()
                        .url("http://192.168.0.102:8080/TXTApp/user/register")
                        .method("POST",body)
                        .build();
                //创建call
                final Call call=client.newCall(request);
                //执行call
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Response response=call.execute();
                            String data=response.body().string();//不能使用toString
                            Log.e(TAG, "run: "+data);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

